package com.running.base.run.util;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;


public class TimeUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(TimeUtils.class);

    // 转换一天的0点
    public static Long parseBeginDateTime(String date) throws ParseException {
        StringBuilder addDate = new StringBuilder();
        addDate.append(date).append(" 00:00:00");
        Date parseDate = DateUtils.parseDate(addDate.toString(), "yyyy-MM-dd HH:mm:ss");
        return parseDate.getTime();
    }

    public static Date parseToDateT(String dateString) throws ParseException {
        if (StringUtil.isNull(dateString)) return null;
        Date parseDate = DateUtils.parseDate(dateString, "yyyy-MM-dd'T'HH:mm");
        return parseDate;
    }

    // 转换到一天的24点
    public static Long parseEndDateTime(String date) throws ParseException {
        StringBuilder addDate = new StringBuilder();
        addDate.append(date).append(" 23:59:59");
        Date parseDate = DateUtils.parseDate(addDate.toString(), "yyyy-MM-dd HH:mm:ss");
        return parseDate.getTime();
    }

    public static Long stameFromyyyymmdd(String date) throws ParseException{
        StringBuilder addDate = new StringBuilder();
        addDate.append(date).append(" 00:00:00");
        Date parseDate = DateUtils.parseDate(addDate.toString(), "yyyyMMdd HH:mm:ss");
        return parseDate.getTime();
    }

    public static String parseTimeStampToLianLian(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(date);
    }

    /**
     * 时间戳转换成时间
     * @param timestamp 毫秒为单位
     * @return
     */
    public static String stampToData(Long timestamp){
        if(10 == timestamp.toString().length()){
            timestamp *=1000;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(timestamp);
        return date;
    }

    public static String reformDateString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(date);
    }

    /**
     * 时间戳换时间
     * @param timestamp
     * @return
     */
    public static String stampToDataTime(Long timestamp){
        String date = "";
        try{
            if(10 == timestamp.toString().length()){
                timestamp *=1000;
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = sdf.format(timestamp);
        }catch (Exception e){
            LOGGER.error("TimeUtils[stampToDataTime]stamp To DataTime exception (date:" + date + ")", e);
        }
        return date;
    }


    public static long getUnixTimesamp(String date, String formateStr) {
        long epoch = 0;
        try {
            epoch = new SimpleDateFormat(formateStr).parse(date).getTime();
        } catch (ParseException e) {
            LOGGER.error("convert unix timestamp exception (date:" + date + ")", e);
        }
        return epoch;
    }


    /**
     * 格式转换 2016-10-23 转换成20161023
     * @param date
     * @return
     * @throws ParseException
     */
    public static String formatToyyyymmdd(String date){
        return date.replaceAll("-", "");
    }

    /**
     * 格式转换 2016-10-23 转换成20161023
     * @param date
     * @return
     * @throws ParseException
     */
    public static String formatToyyyymmddhhmmss(String date){
        return date.replaceAll("-", "").replaceAll(" ","").replaceAll(":", "");
    }

    public static  String getyyyymmddhhmmss(){
        return formatToyyyymmddhhmmss(nowDateTime());
    }

    /**
     * 当前日期yyyy-MM-dd
     * @return
     */
    public static String nowDate(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }

    /**
     * 当前时间 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String nowDateTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    public static int getUnixTimeStamp(Date date){
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime()/1000);
        return Integer.valueOf(timestamp);
    }

    public static String getUnixTimeStampMills(Date date){
        if (null == date) {
            return "";
        }
        return Long.toString(date.getTime());
    }

    public static int addDaysTimeStamp(int add_hour) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, add_hour);
        Date date = c.getTime();
        return getUnixTimeStamp(date);
    }

    public static int addHoursTimeStamp(int add_hour) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.HOUR, add_hour);
        Date date = c.getTime();
        return getUnixTimeStamp(date);
    }

    public static Date getIndexDateTime(int minutes) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MINUTE, minutes);
        Date date = c.getTime();
        return date;
    }

}
